# 字典 dict
# 定义： {key,value} key是不可变切唯一
students = {'name': 'Tom', 'age': 18, 'sex': '男', 18: 19}
# 访问字典
print(students['name'])
print(students['age'])
print(students[18])
# 修改元素
students['sex'] = '女'
students['age'] = 20
print(students)
# 添加元素
students['address'] = '上海'
students['address'] = '北京'
print(students)
# 删除元素
del students['age']
print(students)
# 删除字典
# del students
# print(students)
# len字典函数
print(len(students))
# str输出字典，已可打印的字符串表示
str1 = str(students)
print(str1)
# type返回输入变量的数据类型
print(type(students))
print(type(str1))
# fromkeys 创建一个新字典，以序列seq中元素做字典的值，value为字典所有键对应的初始值
keys = ['name', 'age', 'sex']
dict2 = dict.fromkeys(keys)
print(dict2)
dict3 = dict.fromkeys(keys, 'test')
print(dict3)
# get(key, default=None) 获取指定key的value值
value1 = students.get('name', 'test')
print(value1)
# 弹出元素
address = students.pop("address")
print(address)
print(students)
# 清空元素
students.clear()
print(students)
